<template>
  <div>
    <el-dialog :visible.sync="visible" title="新建平台" width="600px"
               @closed='closedDialog'>
      <el-form ref="platform"
               :model="platform" label-width="80px">
        <el-form-item label="平台名称" :rules="[{ required: true, message: '请输入平台名称', trigger: 'blur' }]">
          <el-input v-model="platform.name"></el-input>
        </el-form-item>
        <el-form-item label="平台ID" :rules="[{ required: true, message: '请输入平台ID', trigger: 'blur' }]">
          <el-input v-model="platform.platformId"></el-input>
        </el-form-item>
        <el-form-item label="启用状态" :rules="[{ required: true }]">
          <el-switch v-model="platform.status"></el-switch>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" style="float: right">立即创建</el-button>
          <el-button @click="closedDialog" style="float: right; margin-right: 10px">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      platform: {
        name: '',
        platformId: '',
        status: false
      },
    }
  },
  methods: {
    onSubmit() {
      if (!this.platform.name) {
        this.$message.error('平台名称不能为空')
        return
      }
      if (!this.platform.platformId) {
        this.$message.error('平台ID不能为空')
        return
      }
      let reg = new RegExp(/[^\w]/)
      if (reg.test(this.platform.platformId)) {
        this.$message.error('平台ID只能输入英文字符, 数字及下划线')
        return
      }
      // 与已有平台校验;
      for (let item of this.$parent.platformTable) {
        //执行代码
        if (item.name == this.platform.name) {
          this.$message.error('平台名称与现有平台重复')
          return
        }
        if (item.platformId == this.platform.platformId) {
          this.$message.error('平台ID与现有平台重复')
          return
        }
      }
      let that = this
      let param = new URLSearchParams();
      param.append("platform", JSON.stringify(this.platform))
      this.axios.post('/pmph/action/platform/insertPlatform', param)
        .then((response) => {
          console.log(response);
          that.closedDialog()
          that.$parent.getPlatforms()
        })
    },
    closedDialog() {
      this.visible = false;
      this.platform.name = ''
      this.platform.platformId = ''
      this.platform.status = false
    }
  }
}
</script>

<style scoped>
</style>
